<?php
/*
 * budingxiaoyuanAPP 应用
 * 
 */
namespace app\api\model;
use think\Validate;
class Send extends \think\Model
{
	public static $sms_config = [
		'appkey'		=> "24592950",//阿里大于APPKEY
		'secretKey' 	=> "38209072ecbc55d4923f497d749424e7",//阿里大于secretKey
		'FreeSignName' 	=> "创客工坊小程序",//短信签名
	];
	public function sms($code,$phone)
	{
	    
	    $data=[
                'param'  => ['code'=>"$code",'product'=>'创客工坊小程序'],
                'mobile'  => "$phone",
                'template'  => "SMS_13010275",
            ];
//		$validate = new Validate([
//			['param','require|array','参数必填|参数必须为数组'],
//			['mobile','require|/1[34578]{1}\d{9}$/','手机号错误|手机号错误'],
//			['template','require','模板id错误'],
//		]);
//		if (!$validate->check($data)) {
//			return $validate->getError();
//		}
		define('TOP_SDK_WORK_DIR', CACHE_PATH.'sms_tmp/');
		define('TOP_SDK_DEV_MODE', false);
		vendor('alidayu.TopClient');
		vendor('alidayu.AlibabaAliqinFcSmsNumSendRequest');
		vendor('alidayu.RequestCheckUtil');
		vendor('alidayu.ResultSet');
		vendor('alidayu.TopLogger');
		$config = self::$sms_config;
        $c = new \TopClient;
        $c ->appkey = $config['appkey'] ;
        $c ->secretKey = $config['secretKey'] ;
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req ->setExtend( "" );
        $req ->setSmsType( "normal" );
        $req ->setSmsFreeSignName( "创客工坊小程序" );
        $req ->setSmsParam( "{validcode:'$code'}" );
        $req ->setRecNum($phone);
        $req ->setSmsTemplateCode( "SMS_103995010" );
        $resp = $c ->execute( $req );

        $result = $this->_simplexml_to_array($resp);
		if(isset($result['code'])){
			return false;
		}
		return true;
	}

	private function _simplexml_to_array($obj)
	{
		if(count($obj) >= 1){
			$result = $keys = [];
			foreach($obj as $key=>$value){
				isset($keys[$key]) ? ($keys[$key] += 1) : ($keys[$key] = 1);
				if( $keys[$key] == 1 ){
					$result[$key] = $this->_simplexml_to_array($value);
				}elseif( $keys[$key] == 2 ){
					$result[$key] = [$result[$key], $this->_simplexml_to_array($value)];
				}else if( $keys[$key] > 2 ){
					$result[$key][] = $this->_simplexml_to_array($value);
				}
			}
			return $result;
		}else if(count($obj) == 0){
			return (string)$obj;
		}
	}
	
}
?>